package com.getpebble.android.common.core.trace;

import android.util.Log;
import com.getpebble.android.common.model.IWatchApp;
import com.getpebble.android.common.model.LoadedApp;
import com.getpebble.android.common.model.LockerApp;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;

public class Analytics {
    private static final String TAG = Analytics.class.getSimpleName();

    public static class MobileAppBehavior {
        public static void logLanguagePackRequest(String bootUrl, String requestUrl) {
            Map<String, Object> dataFields = new HashMap();
            dataFields.put("language_url_boot", bootUrl);
            dataFields.put("language_url_request", requestUrl);
            Analytics.logEvent("mobile-app-behavior", "language_pack_request", dataFields);
        }

        public static void logLanguagePackResponse(int numLanguagePacks) {
            Map<String, Object> dataFields = new HashMap();
            dataFields.put("language_response_count", Integer.valueOf(numLanguagePacks));
            Analytics.logEvent("mobile-app-behavior", "language_pack_response", dataFields);
        }

        public static void logLanguagePacksDisplayed(int numLanguagePacks) {
            Map<String, Object> dataFields = new HashMap();
            dataFields.put("language_displayed_count", Integer.valueOf(numLanguagePacks));
            Analytics.logEvent("mobile-app-behavior", "language_packs_displayed", dataFields);
        }

        public static void logLanguageSettingsOpened() {
            Analytics.logEvent("mobile-app-behavior", "language_settings_opened", null);
        }

        public static void logLanguagePackInstallSuccess(String isoLocale) {
            Map<String, Object> dataFields = new HashMap();
            dataFields.put("iso_locale", isoLocale);
            Analytics.logEvent("mobile-app-behavior", "language_pack_install_success", dataFields);
        }

        public static void logLanguagePackInstallFailed(String isoLocale) {
            Map<String, Object> dataFields = new HashMap();
            dataFields.put("iso_locale", isoLocale);
            Analytics.logEvent("mobile-app-behavior", "language_pack_install_failed", dataFields);
        }

        public static void logAccessabilityDrawerBannerPosted() {
            Analytics.logEvent("mobile-app-behavior", "accessability_drawer_banner_posted", null);
        }

        public static void logAddToLockerAndInstallFromWebviewRequested(LockerApp lockerApp) {
            if (lockerApp != null && !Analytics.isBad(lockerApp.getAppName()) && lockerApp.getUuid() != null) {
                logAddToLockerAndInstallFromWebviewRequested(lockerApp.getAppName(), lockerApp.getUuid().toString().toLowerCase(Locale.US));
            }
        }

        public static void logAddToLockerAndInstallFromWebviewRequested(String appName, String appUuid) {
            if (!Analytics.isBad(appName) && !Analytics.isBadUuid(appUuid)) {
                Map<String, Object> appFields = new HashMap();
                appFields.put("name", appName);
                appFields.put("uuid", appUuid.toLowerCase(Locale.US));
                Map<String, Object> dataFields = new HashMap();
                dataFields.put("app", appFields);
                Analytics.logEvent("mobile-app-behavior", "add_to_locker_and_install_from_webview_requested", dataFields);
            }
        }

        public static void logAddToLockerFromWebviewRequested(LockerApp lockerApp) {
            if (lockerApp != null && !Analytics.isBad(lockerApp.getAppName()) && lockerApp.getUuid() != null) {
                logAddToLockerFromWebviewRequested(lockerApp.getAppName(), lockerApp.getUuid().toString().toLowerCase(Locale.US));
            }
        }

        public static void logAddToLockerFromWebviewRequested(String appName, String appUuid) {
            if (!Analytics.isBad(appName) && !Analytics.isBadUuid(appUuid)) {
                Map<String, Object> appFields = new HashMap();
                appFields.put("name", appName);
                appFields.put("uuid", appUuid.toLowerCase(Locale.US));
                Map<String, Object> dataFields = new HashMap();
                dataFields.put("app", appFields);
                Analytics.logEvent("mobile-app-behavior", "add_to_locker_from_webview_requested", dataFields);
            }
        }

        public static void logAppInstallDetected(IWatchApp watchApp) {
            if (watchApp != null) {
                Map<String, Object> dataFields = new HashMap();
                dataFields.put("app", Analytics.toAnalyticsEventDataAppFields(watchApp));
                Analytics.logEvent("mobile-app-behavior", "app_install_detected", dataFields);
            }
        }

        public static void logButtonTapped(String buttonId, String screenName) {
            if (!Analytics.isBad(buttonId) && !Analytics.isBad(screenName)) {
                Map<String, Object> dataFields = new HashMap();
                dataFields.put("button_id", buttonId);
                dataFields.put("screen", screenName);
                Analytics.logEvent("mobile-app-behavior", "button_tapped", dataFields);
            }
        }

        public static void logDeveloperDrawerItemTapped() {
            Analytics.logEvent("mobile-app-behavior", "developer_drawer_item_tapped", null);
        }

        public static void logDeveloperModeOn() {
            Analytics.logEvent("mobile-app-behavior", "developer_mode_on", null);
        }

        public static void logDeveloperModeOff() {
            Analytics.logEvent("mobile-app-behavior", "developer_mode_off", null);
        }

        public static void logDisconnectedDrawerItemTapped() {
            Analytics.logEvent("mobile-app-behavior", "disconnected_drawer_item_tapped", new HashMap());
        }

        public static void logFwUpdateAvailable(String normalFwVersion, String normalFwVersionShortName, String normalFwVersionTimestamp) {
            if (!Analytics.isBad(normalFwVersion) && !Analytics.isBad(normalFwVersionShortName) && !Analytics.isBad(normalFwVersionTimestamp)) {
                Map<String, Object> firmwareFields = new HashMap();
                firmwareFields.put("fw_version", normalFwVersion);
                firmwareFields.put("fw_version_shortname", normalFwVersionShortName);
                firmwareFields.put("fw_version_timestamp", normalFwVersionTimestamp);
                Map<String, Object> dataFields = new HashMap();
                dataFields.put("firmware", firmwareFields);
                Analytics.logEvent("mobile-app-behavior", "fw_update_available", dataFields);
            }
        }

        public static void logFwUpdateComplete() {
            Analytics.logEvent("mobile-app-behavior", "fw_update_complete", null);
        }

        public static void logFwUpdateFailed() {
            Analytics.logEvent("mobile-app-behavior", "fw_update_failed", null);
        }

        public static void logMyPebbleDetailsDeleteAppTapped() {
            Analytics.logEvent("mobile-app-behavior", "my_pebble_details_delete_app_tapped", new HashMap());
        }

        public static void logMyPebbleConfigSettingsTapped(LoadedApp loadedApp) {
            if (loadedApp != null) {
                Map<String, Object> dataFields = new HashMap();
                dataFields.put("app", Analytics.toAnalyticsEventDataAppFields(loadedApp));
                Analytics.logEvent("mobile-app-behavior", "my_pebble_config_settings_tapped", dataFields);
            }
        }

        public static void logMyPebbleDetailsConfigSettingsTapped(LoadedApp loadedApp) {
            if (loadedApp != null) {
                Map<String, Object> dataFields = new HashMap();
                dataFields.put("app", Analytics.toAnalyticsEventDataAppFields(loadedApp));
                Analytics.logEvent("mobile-app-behavior", "my_pebble_details_config_settings_tapped", dataFields);
            }
        }

        public static void logMyPebbleDetailsLoadTapped(IWatchApp watchApp) {
            if (watchApp != null) {
                Map<String, Object> dataFields = new HashMap();
                dataFields.put("app", Analytics.toAnalyticsEventDataAppFields(watchApp));
                Analytics.logEvent("mobile-app-behavior", "my_pebble_details_load_app_tapped", dataFields);
            }
        }

        public static void logMyPebbleDetailsDeleteAppConfirmedEvent(IWatchApp watchApp) {
            if (watchApp != null) {
                Map<String, Object> dataFields = new HashMap();
                dataFields.put("app", Analytics.toAnalyticsEventDataAppFields(watchApp));
                Analytics.logEvent("mobile-app-behavior", "my_pebble_details_delete_app_confirmed", dataFields);
            }
        }

        public static void logMyPebbleDetailsUnloadTapped(IWatchApp watchApp) {
            if (watchApp != null) {
                Map<String, Object> dataFields = new HashMap();
                dataFields.put("app", Analytics.toAnalyticsEventDataAppFields(watchApp));
                Analytics.logEvent("mobile-app-behavior", "my_pebble_details_unload_app_tapped", dataFields);
            }
        }

        public static void logMobileAppBackgrounded() {
            Analytics.logEvent("mobile-app-behavior", "mobile_app_backgrounded", null);
        }

        public static void logMobileAppDebug(Throwable t, String loggingSource, Map<String, Object> loggingExtendedInfo) {
            Map<String, Object> dataFields = new HashMap();
            if (t != null) {
                dataFields.put("exception_type", t.getClass().getCanonicalName());
                dataFields.put("exception_details", t.getMessage());
                dataFields.put("stack_trace", Log.getStackTraceString(t));
            }
            if (loggingSource != null) {
                dataFields.put("exception_logging_source", loggingSource);
            }
            if (loggingExtendedInfo != null) {
                dataFields.put("logging_extended_info", loggingExtendedInfo);
            }
            Analytics.logEvent("mobile-app-behavior", "mobile_app_debug", dataFields);
        }

        public static void logMobileAppForegrounded() {
            Analytics.logEvent("mobile-app-behavior", "mobile_app_foregrounded", null);
        }

        public static void logScreenOpened(String screenName) {
            if (!Analytics.isBad(screenName)) {
                Map<String, Object> dataFields = new HashMap();
                dataFields.put("screen", screenName);
                Analytics.logEvent("mobile-app-behavior", "screen_appeared", dataFields);
            }
        }

        public static void logSearchTappedEvent() {
            Analytics.logEvent("mobile-app-behavior", "search_tapped", new HashMap());
        }

        public static void logSettingsDrawerItemTappedEvent() {
            Analytics.logEvent("mobile-app-behavior", "settings_drawer_item_tapped", new HashMap());
        }

        public static void logInstalledAndroidApp(Map<String, Object> fields) {
            Trace.verbose(Analytics.TAG, "logInstalledAndroidApp: '" + fields + "'");
            Analytics.logEvent("mobile-app-behavior", "notifications_installed_app", fields);
        }

        public static void logToggleNotificationsEvent(Map<String, Object> fields) {
            Trace.verbose(Analytics.TAG, "logToggleNotificationsEvent: '" + fields + "'");
            Analytics.logEvent("mobile-app-behavior", "notifications_toggled_event", fields);
        }

        public static void logNotificationEvent(Map<String, Object> fields) {
            Trace.verbose(Analytics.TAG, "logNotificationEvent: '" + fields + "'");
            Analytics.logEvent("mobile-app-behavior", "notification_received", fields);
        }

        public static void logNotificationActionInvoked(Map<String, Object> fields) {
            Trace.verbose(Analytics.TAG, "logNotificationActionInvoked: '" + fields + "'");
            Analytics.logEvent("mobile-app-behavior", "notification_action_invoked", fields);
        }

        public static void logNotificationServiceError(String serviceType, boolean error, Throwable throwable) {
            Trace.verbose(Analytics.TAG, "logNotificationServiceError: '" + serviceType + "'");
            Map<String, Object> dataFields = new HashMap();
            dataFields.put("service_type", serviceType);
            dataFields.put("is_error", Boolean.valueOf(error));
            dataFields.put("throwable", Log.getStackTraceString(throwable));
            Analytics.logEvent("mobile-app-behavior", "notification_service_error", dataFields);
        }

        public static void logDataloggingCrcErrorEvent(Map<String, Object> fields) {
            Analytics.logEvent("mobile-app-behavior", "datalogging_crc_error", fields);
        }

        public static void logWatchAnalyticsUploadRejectedEvent(Map<String, Object> fields) {
            Analytics.logEvent("mobile-app-behavior", "watch_analytics_upload_rejected", fields);
        }
    }

    public static class MobileAppOnboarding {
        public static void logOnboardingGrabSomeAppsNextPressed() {
            Analytics.logEvent("mobile-app-behavior", "onboarding_grab_some_apps_next_pressed", null);
        }

        public static void logOnboardingLoginFailedEvent() {
            Analytics.logEvent("mobile-app-behavior", "onboarding_login_failed", null);
        }

        public static void logOnboardingLoginSucceeded(String token) {
            if (!Analytics.isBad(token)) {
                Map<String, Object> dataFields = new HashMap();
                dataFields.put("token", token);
                Analytics.logEvent("mobile-app-behavior", "onboarding_login_succeeded", dataFields);
            }
        }

        public static void logOnboardingMigrationLoadLaterPressed() {
            Analytics.logEvent("mobile-app-behavior", "onboarding_migration_load_later_pressed", null);
        }

        public static void logOnboardingMigrationLoadNowPressed() {
            Analytics.logEvent("mobile-app-behavior", "onboarding_migration_load_now_pressed", null);
        }
    }

    public static class RemoteAppBehavior {
        public static void logAppDeleted(String appUuid) {
            if (!Analytics.isBadUuid(appUuid)) {
                Map<String, Object> appFields = new HashMap();
                appFields.put("uuid", appUuid.toLowerCase(Locale.US));
                Map<String, Object> dataFields = new HashMap();
                dataFields.put("app", appFields);
                Analytics.logEvent("remote-app-behavior", "app_deleted", dataFields);
            }
        }

        public static void logAppInstallFailedHitRemoteSpaceLimit() {
            Analytics.logEvent("remote-app-behavior", "app_install_failed_hit_remote_space_limit", null);
        }

        public static void logAppInstalled(String uuid) {
            if (!Analytics.isBadUuid(uuid)) {
                Map<String, Object> appFields = new HashMap();
                appFields.put("uuid", uuid.toLowerCase(Locale.US));
                Map<String, Object> dataFields = new HashMap();
                dataFields.put("app", appFields);
                Analytics.logEvent("remote-app-behavior", "app_installed", dataFields);
            }
        }

        public static void logJsAppStarted(String appUuid) {
            if (!Analytics.isBadUuid(appUuid)) {
                Map<String, Object> appFields = new HashMap();
                appFields.put("uuid", appUuid.toLowerCase(Locale.US));
                Map<String, Object> dataFields = new HashMap();
                dataFields.put("app", appFields);
                Analytics.logEvent("remote-app-behavior", "js_app_started", dataFields);
            }
        }

        public static void logJsAppStopped(String appUuid) {
            if (!Analytics.isBadUuid(appUuid)) {
                Map<String, Object> appFields = new HashMap();
                appFields.put("uuid", appUuid.toLowerCase(Locale.US));
                Map<String, Object> dataFields = new HashMap();
                dataFields.put("app", appFields);
                Analytics.logEvent("remote-app-behavior", "js_app_stopped", dataFields);
            }
        }

        public static void logSdkAppInstalled() {
            Analytics.logEvent("remote-app-behavior", "sdk_app_installed", null);
            Analytics.logEvent("remote-device-interface", "sdk_app_installed", null);
        }

        public static void logUrlReceivedAppStorePage(String appId) {
            Map<String, Object> appFields = new HashMap();
            appFields.put("id", appId);
            Map<String, Object> dataFields = new HashMap();
            dataFields.put("app", appFields);
            Analytics.logEvent("remote-app-behavior", "url_received_app_store_page", dataFields);
        }
    }

    public static class RemoteDeviceInterface {
        public static void logRemoteDeviceDiscovered(String btAddress) {
            if (!Analytics.isBad(btAddress)) {
                String cleanBTAddr = btAddress.replaceAll("[^a-zA-Z0-9]", "");
                Map<String, Object> dataFields = new HashMap();
                dataFields.put("bt_address", cleanBTAddr);
                Analytics.logEvent("remote-device-interface", "remote_device_discovered", dataFields);
            }
        }

        public static void logRemoteDeviceDisconnected() {
            Analytics.logEvent("remote-device-interface", "remote_device_disconnected", null);
        }

        public static void logRemoteDeviceTimeSync() {
            Analytics.logEvent("remote-device-interface", "remote_device_timesync", null);
        }

        public static void logPrfDetected(String normalFwVersion, String normalFwVersionTimestamp) {
            if (!Analytics.isBad(normalFwVersion) && !Analytics.isBad(normalFwVersionTimestamp)) {
                Map<String, Object> firmwareFields = new HashMap();
                firmwareFields.put("fw_version", normalFwVersion);
                firmwareFields.put("fw_version_timestamp", normalFwVersionTimestamp);
                Map<String, Object> dataFields = new HashMap();
                dataFields.put("firmware", firmwareFields);
                Analytics.logEvent("remote-device-interface", "prf_detected", dataFields);
            }
        }
    }

    public static class Support {
        public static void logSupportCommunityTapped() {
            Analytics.logEvent("support", "support_community_tapped", null);
        }

        public static void logSupportContactSupportTapped() {
            Analytics.logEvent("support", "support_contact_support_tapped", null);
        }

        public static void logSupportDrawerItemTapped() {
            Analytics.logEvent("support", "support_drawer_item_tapped", null);
        }

        public static void logSupportFaqTapped() {
            Analytics.logEvent("support", "support_faq_tapped", null);
        }

        public static void logSupportGettingStartedTapped() {
            Analytics.logEvent("support", "support_getting_started_tapped", null);
        }

        public static void logSupportSuggestSomethingTapped() {
            Analytics.logEvent("support", "support_suggest_something_tapped", null);
        }

        public static void logSupportTestNotificationsTapped() {
            Analytics.logEvent("support", "support_test_notifications_tapped", null);
        }

        public static void logUrlReceivedSupportEmail() {
            Analytics.logEvent("mobile-app-behavior", "url_received_support_email", null);
        }

        public static void logUrlReceivedSupportEmailNoLogs() {
            Analytics.logEvent("mobile-app-behavior", "url_received_support_email_no_logs", null);
        }
    }

    static boolean isBad(String s) {
        return s != null && s.length() < 1;
    }

    static boolean isBadUuid(String uuid) {
        if (uuid == null) {
            return true;
        }
        try {
            UUID.fromString(uuid);
            return false;
        } catch (IllegalArgumentException e) {
            return true;
        }
    }

    static Map<String, Object> toAnalyticsEventDataAppFields(IWatchApp watchApp) {
        Map<String, Object> appFields = new HashMap();
        appFields.put("company_name", watchApp.getCompanyName());
        if (watchApp instanceof LockerApp) {
            appFields.put("has_companion", Boolean.valueOf(((LockerApp) watchApp).hasCompanionApp()));
        } else if (watchApp instanceof LoadedApp) {
            appFields.put("has_companion", Boolean.valueOf(((LoadedApp) watchApp).hasCompanionApp()));
        } else {
            appFields.put("has_companion", Boolean.valueOf(false));
        }
        appFields.put("id", watchApp.getId());
        appFields.put("name", watchApp.getAppName());
        appFields.put("uuid", watchApp.getUuid().toString().toLowerCase(Locale.US));
        if (watchApp instanceof LockerApp) {
            appFields.put("version", ((LockerApp) watchApp).getVersion());
        } else if (watchApp instanceof LoadedApp) {
            appFields.put("version", ((LoadedApp) watchApp).getVersion());
        } else {
            appFields.put("version", null);
        }
        return appFields;
    }

    static void logEvent(String collectionName, String eventType, Map<String, Object> eventData) {
        if (eventData == null) {
            eventData = new HashMap();
        }
        Map<String, Object> event = new HashMap();
        event.put("event", eventType);
        event.put("data", eventData);
        AnalyticsLogger.logEvent("events", event);
    }
}
